import Vue from "vue"
import VueRouter from "vue-router"
import store from "../store"
import vue from '../main'



// use VueRouter
Vue.use(VueRouter)

// import Views
import Home from "../views/Home"
import Login from "../views/Login"
import Register from "../views/Register"
import Recommended from "../views/Recommended"
import Owned from "../views/Owned"
import Products from "../views/Products"
import Profile from "../views/Profile"
import Others from "../views/Others"
import PasswordBack from "../views/PasswordBack"
import AboutUs from "../views/AboutUs"
import InsuranceDetails from "../components/content/InsuranceDetails"
import Search from "../views/Search"
import DigitalPolicy from "../views/DigitalPolicy"
import ReportPolicy from "../views/ReportPolicy"
import FaceVerify from "../views/FaceVerify"
import UploadMaterials from "../views/UploadMaterials"
import QueryProcess from "../views/QueryProcess"
import EditPolicy from "../views/EditPolicy"
import CancelPolicy from "../views/CancelPolicy"
import Buy from "../views/Buy"
import ReportDetails from "../views/ReportDetails"


// 懒加载导入profile中的子组件
const Personal = () => import('../components/content/profilePage/Personal')
const Policy = () => import('../components/content/profilePage/Policy')
const Health = () => import('../components/content/profilePage/Health')
const Messages = () => import('../components/content/profilePage/Messages')
const Settings = () => import('../components/content/profilePage/Settings')

// create routes object
// isLogin is the flag that decides whether the Vue Page needs users to login
const routes = [
    // default

    {path: '/', component: Home , title: '首页',meta: {isLogin: false, title: '首页'}},
    // no need to login
    {path: '/login', component: Login, meta: {isLogin: false}},
    {path: '/register', component: Register, meta: {isLogin: false}},
    {path: '/home', component: Home, meta: {isLogin: false, title: '首页'}},
    {path: '/products', component: Products, meta: {isLogin: false}},
    {path: '/passwordback', component: PasswordBack, meta: {isLogin: false}},
    {path: '/aboutus', component: AboutUs, meta: {isLogin: false}},
    {path: '/insurance_details', component: InsuranceDetails, meta: {isLogin: false}},
    {path: '/search', component: Search, meta: {isLogin: false}},
    {path: '/digital_policy', component: DigitalPolicy, meta: {isLogin: false}},


    // need to login
    {path: '/recommended', component: Recommended, meta: {isLogin: true}},
    {path: '/owned', component: Owned, meta: {isLogin:  true}},
    {path: '/others', component: Others, meta: {isLogin:  true}},
    {path: '/buy', component: Buy, meta: {isLogin:  true}},
    {path: '/report_details', component: ReportDetails, meta: {isLogin:  true}},
    {path: '/profile', component: Profile, meta: {isLogin:  true},
        children:[{path:'',component:Personal},
            {path:'personal',component:Personal},
            {path:'policy',component:Policy},
            {path: 'health',component:Health},{path: 'messages',component:Messages},{path: 'settings',component:Settings}]},
    {path: '/report_policy', component: ReportPolicy, meta: {isLogin:  true}},
    {path: '/query_process', component: QueryProcess, meta: {isLogin:  true}},
    {path: '/edit_policy', component: EditPolicy, meta: {isLogin:  true}},
    {path: '/cancel_policy', component: CancelPolicy, meta: {isLogin:  true}},
    {path: '/face_verify', component: FaceVerify, meta: {isLogin:  true}},
    {path: '/upload_materials', component: UploadMaterials, meta: {isLogin:  true}},
]

// create router
const router = new VueRouter({
    routes,
    mode: 'history'
})

// check the login permission during route jump
router.beforeEach((to, from, next) => {
    // 用户处于登录状态，直接访问所有页面，但页面用户的数据请求由后端的session进行用户校验
    if (store.state.isLogin === true){
        next()
    }else {
        // 非登录状态由页面自己的isLogin决定
        if (to.meta.isLogin){
            vue.$Modal.confirm({
                title: '登录提示',
                content: '点击确定跳转到登录页面',
                onOk: function () {
                    next({path: '/login'})
                },
                onCancel: function () {
                    return false
                }
            })
        }else {
            // no need to login for user
            next()
        }
    }
    if (to.meta.title) document.title = '淘保 - ' + to.meta.title
    else document.title = '淘保'  // 空title或者根本没有声明
})

// scroll to top
// eslint-disable-next-line no-unused-vars

// router.afterEach(route => {
//     window.scroll(0, 0)
// })


export default router